/**
 * 通知系统API接口
 */
import { request } from '@/utils/request'

// 通知类型
export interface Notification {
  id: number
  notification_type: string
  title: string
  content: string
  priority: string
  sender: number
  sender_name: string
  sender_role: string
  recipient: number
  recipient_name: string
  related_object_type?: string
  related_object_id?: number
  is_read: boolean
  read_at?: string
  is_sent: boolean
  sent_at?: string
  created_at: string
}

// 通知模板类型
export interface NotificationTemplate {
  id: number
  name: string
  notification_type: string
  title_template: string
  content_template: string
  is_active: boolean
  created_at: string
  updated_at: string
}

// 用户简单信息类型
export interface UserSimple {
  id: number
  username: string
  first_name: string
  last_name: string
  full_name: string
  role: string
  email: string
}

// 发送通知参数
export interface SendNotificationParams {
  recipient_id: number
  notification_type: string
  title: string
  content: string
  priority?: string
  related_object_type?: string
  related_object_id?: number
}

// 批量发送通知参数
export interface BulkNotificationParams {
  recipient_ids: number[]
  notification_type: string
  title: string
  content: string
  priority?: string
  related_object_type?: string
  related_object_id?: number
}

// 通知查询参数
export interface NotificationQueryParams {
  page?: number
  page_size?: number
  type?: string
  is_read?: boolean
  priority?: string
}

// 用户查询参数
export interface UserQueryParams {
  search?: string
  role?: string
}

// 通知API
export const notificationApi = {
  // 获取通知列表
  getNotificationList: (params?: NotificationQueryParams) =>
    request.get('/notifications/notifications/', params),

  // 获取通知详情
  getNotificationDetail: (id: number) =>
    request.get(`/notifications/notifications/${id}/`),

  // 标记单个通知为已读
  markAsRead: (id: number) =>
    request.post(`/notifications/notifications/${id}/mark_read/`),

  // 标记所有通知为已读
  markAllAsRead: () =>
    request.post('/notifications/notifications/mark_all_read/'),

  // 获取未读通知数量
  getUnreadCount: () =>
    request.get('/notifications/notifications/unread_count/'),

  // 发送单个通知
  sendNotification: (data: SendNotificationParams) =>
    request.post('/notifications/send/', data),

  // 批量发送通知
  sendBulkNotification: (data: BulkNotificationParams) =>
    request.post('/notifications/send-bulk/', data),

  // 获取可发送通知的用户列表
  getUsersForNotification: (params?: UserQueryParams) =>
    request.get('/notifications/users/', params),

  // 删除通知
  deleteNotification: (id: number) =>
    request.delete(`/notifications/notifications/${id}/`)
}

// 通知模板API
export const notificationTemplateApi = {
  // 获取通知模板列表
  getTemplateList: (params?: { type?: string }) =>
    request.get('/notifications/templates/', params),

  // 获取通知模板详情
  getTemplateDetail: (id: number) => 
    request.get(`/notifications/templates/${id}/`),

  // 创建通知模板
  createTemplate: (data: Partial<NotificationTemplate>) => 
    request.post('/notifications/templates/', data),

  // 更新通知模板
  updateTemplate: (id: number, data: Partial<NotificationTemplate>) => 
    request.put(`/notifications/templates/${id}/`, data),

  // 删除通知模板
  deleteTemplate: (id: number) => 
    request.delete(`/notifications/templates/${id}/`)
}

// 通知常量
export const NOTIFICATION_TYPES = [
  { value: 'system', label: '系统通知' },
  { value: 'resume', label: '简历通知' },
  { value: 'interview', label: '面试通知' },
  { value: 'job', label: '岗位通知' }
]

export const NOTIFICATION_PRIORITIES = [
  { value: 'low', label: '低' },
  { value: 'normal', label: '普通' },
  { value: 'high', label: '高' },
  { value: 'urgent', label: '紧急' }
]

// 获取通知类型标签
export const getNotificationTypeLabel = (type: string): string => {
  const item = NOTIFICATION_TYPES.find(t => t.value === type)
  return item ? item.label : type
}

// 获取优先级标签
export const getPriorityLabel = (priority: string): string => {
  const item = NOTIFICATION_PRIORITIES.find(p => p.value === priority)
  return item ? item.label : priority
}

// 获取优先级颜色
export const getPriorityColor = (priority: string): string => {
  const colors: Record<string, string> = {
    low: '#909399',
    normal: '#409EFF',
    high: '#E6A23C',
    urgent: '#F56C6C'
  }
  return colors[priority] || colors.normal
}

// 获取通知类型图标
export const getNotificationTypeIcon = (type: string): string => {
  const icons: Record<string, string> = {
    system: 'Setting',
    resume: 'Document',
    interview: 'ChatDotRound',
    job: 'Briefcase'
  }
  return icons[type] || 'Bell'
}
